package com.ztools.pc.service.impl.information;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.export.StudentExport;
import com.ztools.common.model.pc.information.Classes;
import com.ztools.common.model.pc.information.Student;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.information.StudentMapper;
import com.ztools.pc.service.information.ClassesService;
import com.ztools.pc.service.information.StudentService;
import com.ztools.pc.service.sys.DictService;
import com.ztools.pc.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:一口香菜
 * @Date: 2023/3/19  12:39
 * @Version 1.0
 */
@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private DictService dictService;
    @Resource
    private ClassesService classesService;
    @Resource
    private ExcelUtil excelUtil;

    @Override
    public Page<Student> findList(PageUtil<Student> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>(pageUtil.getParams());
        Page<Student> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<Student> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public boolean add(Student student) {
        return save(student);
    }

    @Override
    public boolean delete(Student student) {
        Student byId = getById(student);
        return removeById(student);
    }

    @Override
    public boolean update(Student student) {
        Student byId = getById(student);
        return updateById(student);
    }

    @Override
    public List<Student> findByObject(Student student) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>(student);
        return list(queryWrapper);
    }

    @Override
    public void exportExcel(HttpServletResponse response, Student student) {
        log.info("exportExcel,student={}", student);
        long count = count();
        long sheetNumber = ExcelUtil.SHEET_NUMBER;
        long pageSize = excelUtil.getPageSize(sheetNumber, count);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>(student);
        for (int i = 0; i < pageSize; i++) {
            Page<Student> pages = new Page<>(i, sheetNumber);
            Page<Student> page = page(pages, queryWrapper);
            List<Student> list = page.getRecords();
            dictionary(list);
            excelUtil.exportExcel(response, i, list, StudentExport.class, "学生基本信息");
        }
    }

    /**
     * 字典翻译
     *
     * @param studentList 对象集合
     */
    public void dictionary(List<Student> studentList) {
        for (Student student : studentList) {
            dictionary(student);
        }
    }

    public void dictionary(Student student) {
        if (student != null) {
            // 性别翻译
            if (student.getSex() != null) {
                student.setSexName(dictService.findByTypeCode(DictUtil.SEX, student.getSex() + ""));
            }
            // 社会面貌翻译
            if (student.getSocialPresence() != null) {
                student.setSocialPresenceName(dictService.findByTypeCode(DictUtil.SOCIAL_PRESENCE, student.getSocialPresence() + ""));
            }
            // 证件类型翻译
            if (student.getDocumentType() != null) {
                student.setDocumentTypeName(dictService.findByTypeCode(DictUtil.DOCUMENT_TYPE, student.getDocumentType() + ""));
            }
            // 民族翻译
            if (student.getNation() != null) {
                student.setNationName(dictService.findByTypeCode(DictUtil.NATION, student.getNation() + ""));
            }
            // 学生状态翻译
            if (student.getStatus() != null) {
                student.setStatusName(dictService.findByTypeCode(DictUtil.STATUS, student.getStatus() + ""));
            }
            // 所在班级翻译
            if (student.getClasses() != null) {
                QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", student.getClasses());
                Classes classes = classesService.getOne(queryWrapper);
                student.setClassesName(classes.getName());
            }
        }
    }

}
